<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 4/24 0024
 * Time: 9:48
 */

namespace app\index\model;


use app\api\model\Meituan;
use think\Model;
use think\Session;

class Jdorder extends Model
{

    /**
     * 获取指定条件的订单
     * @param $condition mixed 条件
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getOrders($condition){
        $where="1";
        if(!empty($condition["status"])){
            $arr=explode(",",$condition["status"]);
            $arr=array_map(function($v){return "'".$v."'";},$arr);
            $where.=" and orderstatus in (".implode(",",$arr).")";
        }
        if(!empty($condition["starttime"])){
            $where.=" and orderstarttime>='".$condition["starttime"]." 00:00:00'";
        }
        if(!empty($condition["endtime"])){
            $where.=" and orderstarttime<='".$condition["endtime"]." 23:59:59'";
        }
        if(!empty($condition["keyword"])){
            $where.=" and orderid='".$condition["keyword"]."'";
        }
        $mtlink=new Meituan();
        $orderdetail=new Jdorderdetail();
        $res=$mtlink->where("shopid",Session::get("shopid"))->find();

        $ePoiId=$res['epoiid'];
        $token=$res['token'];
        $time=time();
        $url="http://api.open.cater.meituan.com/waimai/poi/queryPoiInfo?timestamp=$time&appAuthToken=$token&charset=UTF-8&ePoiIds=$ePoiId";
        $signKey=$mtlink->SignKey($url);
        $url=$url."&sign=".$signKey;
        $json=curl_get($url);
        $shopinfo=json_decode($json,true);
        $data=$this->where($where)->where("producestationnoisv",$res["epoiid"])->where("platform",2)->select();
        foreach ($data as $k=>$v){
            $data[$k]["shopname"]=$shopinfo["data"][0]["noticeInfo"];
            $data[$k]["details"]=$orderdetail->where("pid",$v["id"])->select();
        }
        return $data;
    }

}